<?php
require_once(dirname(__FILE__)."/../include/db_mysql.php");
require_once(dirname(__FILE__)."/../include/httpclient.php");

//该函数将party_email这个商户的点评从commentstoparty表中取出，page代表取出第几页的点评。成功返回点评内容，失败返回false。
function get_dianping( $party_email, $page )
{
    $sql = new SqlClass();
	//连接数据库
	$sql->connect();

	//执行sql语句
	//根据party_email得到partyId。
	$arg = array( 'email_account' => $party_email );
	$partyId = call_serviceurl( 32, $arg); 
	if( $partyId == '' ) //说明没有取到任何的partyId。
	{
	  $partyId = '-1';
	}
	
	//先得到这个商户的全部点评个数。然后转化成总页数。
	$sql_command = "select count(*) as total from commentstoparty where partyId = '$partyId';";
	$r = $sql->exec_sql( $sql_command );
    $res = mysql_fetch_array($r);
	$dianping_num = $res['total'];//$dianping_num保存了该商户全部点评个数。
	//将个数转换成页数，10条算一页，20条算2页，22条算3页这样。
	$totalpage = ceil($dianping_num/10.0);
	//选出符合页数范围内的点评。并且要按时间顺序排列好，越早发的评论越靠后，越新的评论越靠前。
	$first_dianping = ($page-1)*10;
	$sql_command = "SELECT * FROM `commentstoparty` where partyId = $partyId ORDER BY time DESC limit $first_dianping, 10;";
	$r = $sql->exec_sql( $sql_command );//执行sql
	$dianping_arr = array( 'dp_info' => array(), 
	                       'totalpage' => $totalpage
						  );
	while ($row = mysql_fetch_array($r))
	{
	    $arr = array( 'starraty' => $row['comments_star'],
			                    'dianping_txt' => $row['comments_summary'],
								'dianping_time'=> $row['time']
			                   );

		array_push( $dianping_arr['dp_info'], $arr );
    }
	
	$dianping_json = json_encode( $dianping_arr );
	 
	 $sql->close();
	 return $dianping_json;
}

?>